/* Author: Nahuel F. Lema, Maxi Yañez

 */

var js = (function() {
    this.d = document;

    return {
        get: (function() {
            return {
                item: function (v) {
                    return d.getElementsBySelector(v);
                },
                attr: function (v, item) {
                    return (!!item) ? item[0].getAttribute(v) : d.getAttribute(v);
                }
            };
        })(),
        escape: (function() {
            return {
                encode: function (v) {
                    return v.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
                },
                decode: function (v) {
                    return v.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&#039;/g, "\'");
                }
            };
        })()
    }
})();

var scriptsController = function(){
    var obj = {
        defs: {
            baseUrl: '',
            componentsPath: 'resources/jquery/components/'
        },

        setBaseUrl: function(){
            this.defs.baseUrl = js.get.attr('value', js.get.item('#base_url'));
            obj.setBaseUrl = null;
        },

        fixedHeader: {
            defs: {
                container: $(js.get.item('#sticky-menu-container')[0])
            },

            show: function(el){
                el.show();
            },

            hide: function(el){
                el.hide();
            },

            stikyDropdown: function(self, el){
                var list = el.parent().find('ul');

                if (!el.hasClass('active')) {
                    el.addClass('active');
                    self.show(list);
                } else {
                    el.removeClass('active');
                    self.hide(list);
                }
            },

            bind: function(){
                var self = this,
                container = this.defs.container;

                $(window).scroll(function(){
                    if ($(this).scrollTop() < 100 && $(this).width() > 767)
                        self.hide(container);
                    else
                        self.show(container);
                });

                if (!!js.get.item('#mapa-page')[0]) {
                    this.show(container.find('.mapa-page-btns'));
                    this.hide(container.find('.busca-item'));
                }

                container.find('.sticky-dropdown-btn').click(function(e){
                    e.preventDefault();
                    self.stikyDropdown(self, $(this));
                });
            },

            init: function(){
                this.bind();
            }
        },

        bootstrapComponents: {
            bind: function(){
                $(js.get.item('.dropdown-toggle')).dropdown();
                obj.bootstrapComponents = null;
            },

            init: function(){
                this.bind();
            }
        },

        datePickerComponent: {
            defs: {
                el: ''
            },

            load: function(){
                var self = this;

                obj.loadCss('datepicker/css/datepicker.css');

                $.getScript(obj.defs.baseUrl + 'resources/jquery/components/datepicker/js/bootstrap-datepicker.js', function(){
                    self.bind();
                });
            },

            today: function(less){
                var t = new Date(),
                d = (((t.getDay()) < 10) ? '0' + (t.getDay()) : t.getDay()),
                m = (((t.getMonth()+1) < 10) ? '0' + (t.getMonth()+1) : t.getMonth()+1),
                y = (t.getFullYear()-less);
                d = (d == '00') ? '01' : d;
                return  d + '/' + m + '/' + y;
            },

            bind: function(){
                var el = $(this.defs.el), t = (!!el.val())? el.val() : this.today(18), endDate = new Date(parseInt((this.today(17)).split('/')[2]), 0, 0),
                ops = {
                    'value': t,
                    'data-date': t
                };

                el.attr(ops);

                el.datepicker({
                    'format': 'dd/mm/yyyy',
                    'weekStart': 1
                }).on('changeDate', function(ev){
                    if (ev.date.valueOf() > endDate.valueOf()){
                        alert('La fecha no puede ser mayor a ' + endDate.getFullYear());
                        el.attr(ops);
                    }

                    el.datepicker('update');
                });

                obj.datePickerComponent = null;
            },

            init: function(){
                this.defs.el = js.get.item('.datepicker')[0];

                if (!!this.defs.el)
                    this.load();
            }
        },

        loadCss: function(v){
            var link = document.createElement('link');
            link.setAttribute('type', 'text/css');
            link.setAttribute('rel', 'stylesheet');
            link.setAttribute('href', obj.defs.baseUrl + 'resources/jquery/components/' + v);
            $('head').append(link);
        },

        loadController: function(v){
            $.getScript(this.defs.baseUrl + 'resources/js/' + v + 'Controller.js');
            obj.loadController = null;
        },
	
        loader: {
            defs: {
                el: $('<div class="circle-container rad-border">'
                    + '<div class="circle-loader rad-border"></div>'
                    + '<div class="circle-content rad-border"></div>'
                    + '<div class="center-circle-image rad-border"></div>'
                    + '</div>'),
                ie: '\
                    <img id="gifLoader" src="'+$('#base_url').val()+'resources/img/loader.gif" />\
                '
            },

            append: function(container, radius){
                var el = ($.browser.msie)?this.defs.ie:this.defs.el;
                
                if (!$.browser.msie) {
                    container.addClass(radius + '-loader');
                    if (!container.find('.circle-container').length) {
                        container.append(el);
                    } else {
                        container.find('.circle-container').show();
                    }
                } else {
                    if (container.find('#gifLoader').length) {
                        container.fadeIn(300)
                    } else {
                        container.append(el);
                    }
                }
                
                container = el = radius = null;
            },
	    
            remove: function(container){
                if (!$.browser.msie) {
                    container.find('.circle-container').hide();
                } else {
                    container.fadeOut(800);
                }
                container.removeClass('tiny-loader small-loader medium-loader big-loader');
            }
        },
        
        fixedLoader: {
            defs: {
                el: '\
                    <div class="circle-container rad-border">\
                    <div class="circle-loader rad-border"></div>\
                    <div class="circle-content rad-border"></div>\
                    <div class="center-circle-image rad-border"></div>\
                    </div>\
                ',
                ie: '\
                    <img id="gifLoader" src="'+$('#base_url').val()+'resources/img/fixed-loader.gif" />\
                '
            },
            isAppend:false,
            show: function () {
                var el = ($.browser.msie)?this.defs.ie:this.defs.el;
                if (this.isAppend) {
                    $('#fixedLoader').show();
                } else {
                    $('body').prepend('<div id="fixedLoader">'+el+'</div>');
                    $(window).resize(this.ajustarAltoBg);
                    this.isAppend = true;
                }
                this.ajustarAltoBg();
            },
            hide: function () {
                $('#fixedLoader').hide();
            },
            ajustarAltoBg: function () {
                var alto = $(window).height();
                $('#fixedLoader').height(alto)
            }
        }
    };

    obj.setBaseUrl();

    obj.fixedHeader.init();

    obj.bootstrapComponents.init();

    obj.datePickerComponent.init();

    if (!!js.get.item('#home-page')[0]) obj.loadController('home');

    else if (!!js.get.item('#organicos-page')[0]) obj.loadController('organicos');

    else if (!!js.get.item('#links-amigos-page')[0]) obj.loadController('linksAmigos');

    else if (!!js.get.item('#nos-apoyan-page')[0]) obj.loadController('nosApoyan');

    else if (!!js.get.item('#prensa-page')[0]) obj.loadController('prensa');

    else if (!!js.get.item('#iniciativas-page')[0]) obj.loadController('iniciativas');

    else if (!!js.get.item('#como-funciona-page')[0]) obj.loadController('comoFunciona');

    //else if (!!js.get.item('#panel-usuario-page')[0]) obj.loadController('panelUsuario');

    else if (!!js.get.item('#mapa-page')[0]) obj.loadController('mapa');
    
    else if (!!js.get.item('#mapa-test-page')[0]) obj.loadController('mapaTest');

    else if (!!js.get.item('#voluntarios-page')[0]) obj.loadController('voluntarios');

    else if (!!js.get.item('#ingresar-page')[0]) obj.loadController('ingresar');

    else if (!!js.get.item('#cooperativas-page')[0]) obj.loadController('cooperativas');

    else if (!!js.get.item('#contacto-page')[0]) obj.loadController('contacto');

    else if (!!js.get.item('#comparti-tu-punto-page')[0]) obj.loadController('compartiTuPunto');

    else if (!!js.get.item('#crea-tu-cuenta-page')[0]) obj.loadController('creaTuCuenta');

    else if (!!js.get.item('#yo-reuso-page')[0]) obj.loadController('yoReuso');

    else if (!!js.get.item('#informacion-page')[0]) obj.loadController('informacion');
    
    else if (!!js.get.item('#faq-container')[0]) obj.loadController('faq');

    return scriptsController = obj;
}

var animatedLogo = {
    init: function () {
        $('#logo-donde-reciclo').mouseenter(function () {
            $(this).addClass('swing');
	})
	$('#logo-donde-reciclo').mouseleave(function () {
            setTimeout(desanimar, 300)
	})
	function desanimar() {
            $('#logo-donde-reciclo').removeClass('swing');
	}
    }
}

$(function(){
    scriptsController();
    animatedLogo.init();
})






